package com.gang.ssm.bos.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.activiti.engine.IdentityService;
import org.activiti.engine.impl.persistence.entity.UserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gang.ssm.bos.common.Page;
import com.gang.ssm.bos.core.toolbox.kit.CollectionKit;
import com.gang.ssm.bos.system.bean.Role;
import com.gang.ssm.bos.system.bean.User;
import com.gang.ssm.bos.system.mapper.RoleMapper;
import com.gang.ssm.bos.system.mapper.UserMapper;
import com.gang.ssm.bos.system.service.UserService;

@Service
public class UserServiceImpl implements UserService {

	@Autowired
	private UserMapper userMapper;
	
	@Autowired
	private RoleMapper roleMapper;
	
	@Autowired
	private IdentityService identityService;

	@Override
	public void pageQuery(Page<User> pageBean) {
		int total = (int) userMapper.countByExample(null);
		List<User> users = userMapper.findByLimit((pageBean.getPageNo() - 1) * 
				pageBean.getPageSize(),pageBean.getPageSize());
		
		pageBean.setTotalCount(total);
		pageBean.setResult(users);
	}

	@Override
	public User findByUsername(String username) {
		return (User) userMapper.findByUsername(username);
	}

	@Override
	public List<Integer> findMenuIdsByUserId(Integer id) {
		return userMapper.findMenuIdsByUserId(id);
	}

	@Override
	public User findById(Integer id) {
		return userMapper.findById(id);
	}

	@Override
	public void saveOrUpdate(User user, String roleIds) {
		
		if(user.getId() != null) {
			
			userMapper.updateByPrimaryKeySelective(user);
			
			//删除act_id_membership表中与给userid关联的记录
			List<String> userRoleNames = roleMapper.findNameByUserId(user.getId());
			if(!CollectionKit.isEmpty(userRoleNames)) {
				for (String roleName : userRoleNames) {
					identityService.deleteMembership(user.getId().toString(), roleName);
				}
				
				userMapper.deleteRoleIdAndUserIdByUserId(user.getId());
			}
			
		}else {
			userMapper.insertSelective(user);
			
			org.activiti.engine.identity.User Actuser = new UserEntity(user.getId().toString());
			identityService.saveUser(Actuser);
		}
		
		//保存用户和角色到user_role表
		List<Integer> roleIdList = new ArrayList<Integer>();
		for (String roleIdStr : roleIds.split(",")) {
			roleIdList.add(Integer.valueOf(roleIdStr));
		}
		userMapper.insertUserAndRole(user.getId(), roleIdList);
		
		//关联activiti user和group
		for (Integer roleId : roleIdList) {
			Role role = roleMapper.selectByPrimaryKey(roleId);
			
			identityService.createMembership(user.getId().toString(), role.getName());
		}
		
	}

	@Override
	public void deleteById(Integer userId) {
		
		userMapper.deleteRoleIdAndUserIdByUserId(userId);
		userMapper.deleteByPrimaryKey(userId);
		
		//同时删除activiti 中act_id_user表和act_id_membership表与userId相关联的记录
		identityService.deleteUser(userId.toString());
		List<String> userRoleNames = roleMapper.findNameByUserId(userId);
		for (String roleName : userRoleNames) {
			identityService.deleteMembership(userId.toString(), roleName);
		}
	}
}
